package com.chuangjiangx.member.manager.basic.web.controller;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.member.manager.basic.web.response.Response;
import com.chuangjiangx.member.manager.common.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Slf4j
public class ControllerExceptionHandler {

    private String msg = "您没有代该特约商户发起API退款的权限，需该特约商户进行授权，相关授权流程请参照http://kf.qq.com/faq/120911VrYVrA150929imAfuU.html的指引";
    private int maxLength = 50;

    /**
     * 基于@ExceptionHandler异常处理.
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Response exceptionHandle(Throwable throwable) {
        log.error("", throwable);
        if (throwable instanceof BaseException) {
            BaseException ex = (BaseException) throwable;
            return ResponseUtils.error(ex.getErrCode(), ex.getErrMessage());
        } else if (throwable instanceof com.chuangjiangx.commons.exception.BaseException){
            com.chuangjiangx.commons.exception.BaseException ex = (com.chuangjiangx.commons.exception.BaseException) throwable;
            return ResponseUtils.error(ex.getErrCode(), ex.getErrMessage());
        }else if (throwable instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException mane = (MethodArgumentNotValidException) throwable;
            BindingResult bindingResult = mane.getBindingResult();
            return ResponseUtils.error("-1", bindingResult.getAllErrors().get(0).getDefaultMessage());
        } else if (throwable instanceof BindException) {
            BindException bindException = (BindException) throwable;
            BindingResult bindingResult = bindException.getBindingResult();
            return ResponseUtils.error("-1", bindingResult.getAllErrors().get(0).getDefaultMessage());
        } else if (throwable instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException ex = (MissingServletRequestParameterException) throwable;
            return ResponseUtils.error("-1", "参数缺失：" + ex.getParameterName());
        } else if (throwable instanceof IllegalArgumentException) {
            return ResponseUtils.error("-1", throwable.getMessage());
        } else {
            String msg = throwable.getMessage();
            if (msg.length() > maxLength) {
                return ResponseUtils.error("-1", "服务端发生错误！");
            } else {
                return ResponseUtils.error("-1", throwable.getMessage());
            }
        }
    }
}
